package com.shanghaionstar.home.presenter;

import android.text.TextUtils;

import com.onstar.cn.auth.userProfile.FeatureEnum;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.function.FunctionInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.home.function.ShortCutManager;
import com.shanghaionstar.home.iview.IEditShortCutView;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

/**
 * Created by 6335yz on 2016/6/24.
 */
public class EditShortCutPresenter extends BasePresenter<Object> {
    private List<FunctionInfo> mEditList;
    private List<FunctionInfo> mHotSeatList;
    private IEditShortCutView mShortCutView;
    private BaseActivity mContext;
    private ShortCutManager shortCutManager;

    public EditShortCutPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Object> view) {
        super.setView(view);
        mShortCutView = (IEditShortCutView) view;
        mContext = (BaseActivity) mShortCutView.onActivity();
        shortCutManager = new ShortCutManager(mContext);
    }

    public void getShortCutData() {
        mEditList = shortCutManager.getData();
        mHotSeatList = shortCutManager.getShortCuts();
        mHotSeatList.remove(mHotSeatList.size() - 1);
        mEditList.removeAll(mHotSeatList);
        removeRemoteFunOnGen9(mEditList);
        mShortCutView.initView(mEditList, mHotSeatList);
    }

    public void saveData(TreeSet<FunctionInfo> hotSeatList) {
        shortCutManager.updateAll(hotSeatList);
    }

    /**
     * 某些Gen9用户可能没有远程启动和取消功能
     */
    private void removeRemoteFunOnGen9(List<FunctionInfo> mFunctionInfos) {
        if (!BusinessUtil.isServiceFeature(FeatureEnum.START.value(), mContext) && !isVistorOrAnonymous()) {
            String[] remoteArr = mContext.getResources().getStringArray(R.array.remote_control_list);
            Iterator<FunctionInfo> iterator = mFunctionInfos.iterator();
            while (iterator.hasNext()) {
                FunctionInfo info = iterator.next();
                String title = info.getTitle();
                if (title.equalsIgnoreCase(remoteArr[0]) || title.equalsIgnoreCase(remoteArr[1])) {
                    iterator.remove();
                }
            }
        }
    }

    private boolean isVistorOrAnonymous() {
        boolean isVistorOrAnonymous = !ApplicationUtil.getInstance().isLogin();
        String loginRole = ApplicationUtil.getInstance().getLoginRole();
        isVistorOrAnonymous |= (!TextUtils.isEmpty(loginRole) && loginRole.equalsIgnoreCase(Constants.VISITOR));
        return isVistorOrAnonymous;
    }


    private void removeHotseat(List<FunctionInfo> functionInfos, Collection<FunctionInfo> mHotSeatList) {
        Iterator<FunctionInfo> iterator = functionInfos.iterator();
        while (iterator.hasNext()) {
            FunctionInfo info = iterator.next();
            for (FunctionInfo hotInfo : mHotSeatList) {
                hotInfo.setInHotseat(true);
                if (hotInfo.equals(info))
                    iterator.remove();
            }
        }

    }

    public void reFreshEditList(TreeSet<FunctionInfo> mHotList) {
//        ShortCutManager shortCutManager = ShortCutManager.getInstance(mContext);
        mEditList = shortCutManager.getData();
        removeHotseat(mEditList, mHotList);
        removeRemoteFunOnGen9(mEditList);
        mShortCutView.reFreshEditList(mEditList);
    }

    public void onDestory() {
        shortCutManager = null;
        if (mHotSeatList != null) {
            mHotSeatList.clear();
            mHotSeatList = null;
        }
    }
}
